<template>
    <div class="app-container">
      <el-form :model="queryParams" class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <div class="from-item">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="queryParams.tagName" @keyup.enter.native="handleQuery" placeholder="请输入标签名称" clearable/>
            </el-form-item>
            <el-form-item label="设置时间" prop="beginTime">
              <el-date-picker v-model="chatTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </div>
          <div class="from-Btn">
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" style="background-color: #0078fb;color: #fff;border-color: #0078fb;" plain icon="el-icon-folder-add" size="mini" @click="handleAdd">添加标签</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" style="background-color: #0078fb;color: #fff;border-color: #0078fb;" plain icon="el-icon-circle-close" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" style="background-color: #0078fb;color: #fff;border-color: #0078fb;" plain icon="el-icon-upload2" size="mini" @click="handleExport">批量导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="wcTagsInfoList" @selection-change="handleSelectionChange" :header-cell-style="{ background: '#fcfcfd', color: '#000' }" border>
        <el-table-column type="selection" width="55" align="center" :show-overflow-tooltip="true"/>
        <el-table-column label="标签名" width="180" prop="tagName" align="center" :show-overflow-tooltip="true"/>
        <el-table-column label="设置日期" prop="createTime" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width" >
          <template slot-scope="scope">
            <el-button size="mini" type="text" style="background-color: #0078fb;color: #fff;width: 50px;" icon="el-icon-document" @click="goToUrl(scope.row)">详情</el-button>
            <el-button size="mini" type="text" style="background-color: #fb9d52;color: #fff;width: 50px;" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            <el-button size="mini" type="text" style="background-color: #fb9d52;color: #fff;width: 50px;" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加本地标签信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <div class="dia-box">
          <span style="font-size: 14px;margin-right: 10px;color: #333;">新建标签</span>
          <div style="width: 280px;margin: 10px 0 50px 0;">
           <el-input placeholder="请输入标签名" v-model="keyValue" class="input-with-select">
            <el-button slot="append" @click="setAdd">保存</el-button>
           </el-input>
          </div>
          <div class="dia-box-lable">
             <span style="font-size: 14px;color: #9aa5aa;">已添加：</span>
             <div v-for="item,index in name" :key="index" class="lable-box">{{ item }}<i @click="delIdx(index)" class="el-icon-close" style="margin-left: 10px;"></i></div>
          </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="AddName" style="margin-left: 30px;">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改本地标签信息对话框 -->
    <el-dialog title="修改标签" :visible.sync="opens" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入标签名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </div>
  </template>
  
  <script>
  

import request from '@/utils/request'

export default {
  name: "WcTagsInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 本地标签信息表格数据
      wcTagsInfoList: [{id:1,tagName:'活跃',createTime:'2024-05-20 12:23'}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagName: null,
        beginTime: null,
        endTime: null,
      },
      chatTime:[],
      // 表单参数
      form: {
        id: null,
        tagName: null,
      },
      // 表单校验
      rules: {
        tagName: [
          { required: true, message: "标签名称不能为空", trigger: "blur" }
        ],
      },
      keyValue:"",
      name:[],
      nameList:[],
    };
  },
  created() {
    
  },
  mounted(){
    this.getList();
  },
  methods: {
    goToUrl(params){
      this.$router.push({ path:"/tag/detial",name:"tag-detial",query:{id:params.tagName}}).catch(()=>{});
    },
    delIdx(idx){
      this.name.splice(idx,1)
      this.nameList.splice(idx,1)
    },
    setAdd(){
       if(this.keyValue!=''&&this.keyValue!=null){
          if(this.name.indexOf(this.keyValue)==-1){
             this.name.push(this.keyValue)
             this.nameList.push({tagName: this.keyValue})
             this.keyValue=""
          }else{
            this.$message({
                message: '标签名已存在！',
                type: 'warning'
            });
          }
       }else{
        this.$message({
            message: '请输入标签名！',
            type: 'warning'
        });
       }
    },
    /** 查询本地标签信息列表 */
    getList() {
      this.loading = true;
      listWcTagsInfo(this.queryParams).then(res => {
        if (res['success']) {
          const dt = res['data'];
          this.total = dt['total'];
          this.wcTagsInfoList = dt['records'];
        }
        this.loading = false;
      });
      setTimeout(()=>{
        this.loading = false;
      },2000)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.name=[]
      this.nameList=[]
      this.keyValue=""
      this.form={
        id: null,
        tagName: null,
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tagName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.beginTime=null
      this.queryParams.endTime=null
      if(this.chatTime!=''&&this.chatTime!=null){
        this.queryParams.beginTime=this.chatTime[0]+' 00:00:00'
        this.queryParams.endTime=this.chatTime[1]+' 23:59:59'
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.chatTime=[]
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.name=[]
      this.nameList=[]
      this.keyValue=""
      this.open = true;
      this.title = "添加标签";
    },
    AddName(){
       if(this.nameList!=''&&this.nameList!=null){
        addWcTagsInfo(this.nameList).then(res=>{
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
        })
       }else{
        this.$message({
            message: '请添加标签名！',
            type: 'warning'
        });
       }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {id:row.id,tagName:row.tagName}
      this.opens = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWcTagsInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.opens = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选本地标签').then(function() {
        return delWcTagsInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wxassistant/wctags_info/export', {
        ...this.queryParams
      }, `wcTags_${new Date().getTime()}.xlsx`)
    }
  }
};
/** js请求 调用 */

// 查询本地标签信息列表
export function listWcTagsInfo(query) {
  return request({
    url: '/wxassistant/wctags_info/page',
    method: 'get',
    params: query
  })
}

// 查询本地标签信息详细
// export function getWcTagsInfo(id) {
//   return request({
//     url: '/arbit/wctags_info/queryById/' + id,
//     method: 'get'
//   })
// }

// 批量本地标签信息
export function addWcTagsInfo(data) {
  return request({
    url: '/wxassistant/wctags_info/addList',
    method: 'post',
    data: data
  })
}

// 修改本地标签信息
export function updateWcTagsInfo(data) {
  return request({
    url: '/wxassistant/wctags_info/edit',
    method: 'put',
    data: data
  })
}

// 删除本地标签信息
export function delWcTagsInfo(id) {
  return request({
    url: '/wxassistant/wctags_info/delete/' + id,
    method: 'delete'
  })
}
</script>
<style scoped lang="scss">
  ::v-deep .el-dialog__title{
      font-size: 14px;
      font-weight: 600;
      color: #333;
  }
  .app-container{
        background-color: #f6f9fb;
        .frominfo{
          display: flex;background-color: #fff;padding: 10px;padding-bottom: 0; margin-bottom: 20px;
          .from-item{
            display: flex;flex-wrap: wrap;
          }
          .from-Btn{
            display: flex;flex-direction: column;align-items: center;justify-content: center;width: 20%;
          }
        }
  }   
  .dia-box{
      padding: 20px;
      display: flex;
      flex-direction: column;
  }
  .dia-box-lable{
      width:90%;
      border-radius: 5px;
      border:1px solid #eee;
      min-height: 100px;
      display: flex;
      flex-wrap: wrap;
      padding: 10px;
      margin-bottom: 20px;
      .lable-box{
        padding: 0 5px;
        height: 20px;
        line-height: 20px;
        margin-right: 10px;
        border: 1px solid #dcdcdc;
        background-color: #ebf4ff;
        font-size: 12px;
        border-radius: 3px;
      }
      .select{
        color: #fff;
        background-color: #4095e5;
      }
  }
  .dialog-footer{
    display: flex;
    justify-content: center;
  } 
</style>